package com.settlement.system.common.util.common;

import com.settlement.system.context.SpringContextHolder;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author author
 */
public class GenerateBillNoUtil {

    private static RedisTemplate redisTemplate;

    static {
        redisTemplate = SpringContextHolder.getBean("redisTemplate", RedisTemplate.class);
    }

    public static String getUinqueBillNo(String billType,String ruleNo) {
        return String.format("%s%s%09d", billType, ruleNo,redisTemplate.opsForValue().increment(billType + ruleNo, 1));
    }

    public static String getUinquelossReasonDtlNo(String billType) {
        return String.format("%s%03d", billType,redisTemplate.opsForValue().increment(billType, 1));
    }

    /**
     * 例：bizType=ORDER，ruleNo=20210715，prefix=AM,length=4，则返回结果为AM202107150001
     * @param bizType
     * @param ruleNo
     * @param length
     * @return
     */
    public static String getUinqueNo(String bizType,String ruleNo,String prefix,String length) {
        return String.format("%s%s%0"+length+"d", prefix, ruleNo,redisTemplate.opsForValue().increment(bizType + "_" + prefix, 1));
    }

    /**
     * 按业务类型，指定长度，递增序列号
     * 例：bizType=ORDER,prefix=AM,length=4,则返回结果为AM0001
     * @param bizType
     * @param length
     * @return
     */
    public static String getUinqueNoByBizType(String bizType,String prefix, String length) {
        return String.format("%s%0"+length+"d", prefix,redisTemplate.opsForValue().increment(bizType+"_"+prefix, 1));
    }

    public enum BizTypeEnum {
        /**
         * 权限角色组编码
         */
        ACS_PERMISSION_ROLE_GROUP("ACS_PERMISSION_ROLE_GROUP","权限角色组编码", "RG");

        BizTypeEnum(String code, String name, String prefix) {
            this.code = code;
            this.name = name;
            this.prefix = prefix;
        }

        private final String code;
        private final String name;
        private final String prefix;

        public String getCode() {
            return code;
        }

        public String getName() {
            return name;
        }

        public String getPrefix() {
            return prefix;
        }
    }
}
